package com.cmc.cloud.cmclink.doc.service.impl.so;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cmc.cloud.cmclink.doc.api.document.req.so.SoForecastRespVO;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.SoForecastErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.SoForecastConvert;
import com.cmc.cloud.cmclink.doc.entity.SoForecastDO;
import com.cmc.cloud.cmclink.doc.mapper.SoForecastMapper;
import com.cmc.cloud.cmclink.doc.service.so.SoForecastService;
import com.cmc.cloud.cmclink.doc.vo.sovo.soforecastvo.*;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * 订舱预配信息 Service 实现类
 *
 * @author 招商轮船-管理员
 */
@Service
@Validated
public class SoForecastServiceImpl implements SoForecastService {

    @Resource
    private SoForecastMapper soForecastMapper;

    @Override
    public void createSoForecast(SoForecastCreateReqVO createReqVO) {
        // 插入
        SoForecastDO soForecast = SoForecastConvert.INSTANCE.convert(createReqVO);
        soForecastMapper.insert(soForecast);
    }

    @Override
    public void createBatch(List<SoForecastCreateReqVO> createReqVOList) {
        //批量插入
        List<SoForecastDO> soVslvoyList = SoForecastConvert.INSTANCE.convertBatch(createReqVOList);
        soForecastMapper.insertBatch(soVslvoyList);
    }

    @Override
    public List<SoForecastSumVO> selectSumBySoIds(List<Long> soIds) {
        if (CollectionUtil.isEmpty(soIds)) {
            return new ArrayList<>();
        }
        return soForecastMapper.selectSumBySoIds(soIds);
    }

    @Override
    public void createOrUpdateAndDeleteBatch(Long soId, List<SoForecastUpdateReqVO> newList, List<SoForecastRespVO> oldList) {
        // 无效数据直接返回
        if (CollectionUtil.isEmpty(newList) && CollectionUtil.isEmpty(oldList)) {
            return;
        }
        // 默认值
        if (CollectionUtil.isEmpty(newList)) {
            newList = new ArrayList<>();
        }
        if (CollectionUtil.isEmpty(oldList)) {
            oldList = new ArrayList<>();
        }
        List<SoForecastUpdateReqVO> createList = new ArrayList<>();
        List<SoForecastUpdateReqVO> updateList = new ArrayList<>();
        // 历史数据
        Set<Long> oldIds = oldList.stream().map(e -> e.getId()).collect(Collectors.toSet());
        newList.forEach(e -> {
            if (e.getId() != null) {
                if (oldIds.contains(e.getId())) {
                    // 正常更新
                    updateList.add(e);
                    oldIds.remove(e.getId()); // 剩下的数据需要删除
                } else {
                    // 数据更新非法
                    throw ServiceExceptionUtil.exception(SoForecastErrorCodeConstants.SO_FORECAST_NOT_EXISTS);
                }
            } else {
                e.setSoId(soId);
                createList.add(e);
            }
        });
        if (CollectionUtil.isNotEmpty(oldIds)) {
            // 没有匹配上的就是前端删除的
            soForecastMapper.deleteBatchIds(oldIds);
        }
        if (CollectionUtil.isNotEmpty(createList)) {
            List<SoForecastDO> soForecastCreateList = SoForecastConvert.INSTANCE.convertBatchUpdate(createList);
            soForecastMapper.insertBatch(soForecastCreateList);
        }
        if (CollectionUtil.isNotEmpty(updateList)) {
            List<SoForecastDO> soForecastUpdateList = SoForecastConvert.INSTANCE.convertBatchUpdate(updateList);
            soForecastMapper.updateBatch(soForecastUpdateList);
        }

    }

    @Override
    public void updateSoForecast(SoForecastUpdateReqVO updateReqVO) {
        // 校验存在
        validateSoForecastExists(updateReqVO.getId());
        // 更新
        SoForecastDO updateObj = SoForecastConvert.INSTANCE.convert(updateReqVO);
        soForecastMapper.updateById(updateObj);
    }

    @Override
    public void deleteSoForecast(Long id) {
        // 校验存在
        validateSoForecastExists(id);
        // 删除
        soForecastMapper.deleteById(id);
    }

    private void validateSoForecastExists(Long id) {
        if (soForecastMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(SoForecastErrorCodeConstants.SO_FORECAST_NOT_EXISTS);
        }
    }

    @Override
    public SoForecastDO getSoForecast(Long id) {
        return soForecastMapper.selectById(id);
    }

    @Override
    public List<SoForecastDO> getSoForecastList(Collection<Long> ids) {
        return soForecastMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<SoForecastDO> getSoForecastPage(SoForecastPageReqVO reqVO) {
        LambdaQueryWrapperX<SoForecastDO> queryWrapper = new LambdaQueryWrapperX<>();
        return soForecastMapper.selectPage(reqVO, queryWrapper);
    }

    @Override
    public void deleteBySoIds(Collection<Long> sIds) {
        LambdaUpdateWrapper<SoForecastDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(SoForecastDO::getSoId, sIds);
        soForecastMapper.delete(updateWrapper);
    }

    @Override
    public List<SoForecastDO> seleteBySoId(Long soId) {
        LambdaQueryWrapper<SoForecastDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SoForecastDO::getSoId, soId);
        queryWrapper.orderByAsc(SoForecastDO::getId);
        return soForecastMapper.selectList(queryWrapper);
    }

    @Override
    public List<SoForecastDO> seleteBySoIdList(List<Long> soIdList) {
        LambdaQueryWrapper<SoForecastDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SoForecastDO::getSoId, soIdList);
        return soForecastMapper.selectList(queryWrapper);
    }
}
